package com.example.peanutsubway.dao.impl;

import com.example.peanutsubway.dao.MenuInfoDao;
import com.example.peanutsubway.entity.Menu;
import com.example.peanutsubway.entity.MenuInfo;
import com.example.peanutsubway.entity.MenuRole;

import com.example.peanutsubway.util.JDBCUtils;
import com.example.peanutsubway.util.PageInfo;
import com.example.peanutsubway.vo.MenuType;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

/**
 * @Author：liulei
 * @Version：1.0
 * @Date：2021/10/11-17:50
 * @Since:jdk1.8
 * @Description:
 */
public class MenuInfoDaoImpl implements MenuInfoDao {
    private Connection connection;

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    @Override
    public List<MenuInfo> findParentMenu(Integer roleId) {
        String sql = "select m.* from pn_menu_role mr, pn_menu m " +
                "where m.mid = mr.menuid " +
                "and mr.rid = ? and m.parent_id = 0 AND state = 1";
        return JDBCUtils.find(sql, MenuInfo.class, roleId);
    }

    @Override
    public List<MenuInfo> findSonMenu(Integer roleId, Integer parentId) {
        String sql = "select m.* from pn_menu_role mr, pn_menu m " +
                "where m.mid = mr.menuid " +
                "and mr.rid = ? and m.parent_id = ? AND state = 1";
        return JDBCUtils.find(sql, MenuInfo.class, roleId, parentId);
    }

    @Override
    public List<MenuInfo> showUserMenuByRoleMinId(Integer roleId) {
        String sql = "select m.* from pn_menu_role mr, pn_menu m where m.mid = mr.menuid and mr.rid = ? AND state = 1";
        return JDBCUtils.find(sql, MenuInfo.class, roleId);
    }

    /**
     * 待解决sql处理问题
     *
     * @param menuId
     * @return
     */
    @Override
    public Boolean addTree(Integer menuId,Integer rid) {
        String sql = "INSERT into pn_menu_role(id,rid,menuid) VALUES(?,?,?)";
        return JDBCUtils.insert(connection,sql,null,rid,menuId).intValue()> 0;
    }

    @Override
    public Integer lastTreeId(){
        String sql = "select * from pn_menu_role order by rid DESC limit 1";
        MenuRole o = JDBCUtils.get(sql, MenuRole.class);
        return o.getRid();
    }

    @Override
    public List<Menu> findAllMenu(Menu menu, PageInfo pageInfo) {
        String sql = "SELECT p2.*,p.menu_name pname FROM pn_menu p " +
                "RIGHT JOIN pn_menu p2 " +
                "ON p.mid=p2.parent_id WHERE p2.state = 1 limit ?,?";
        return JDBCUtils.find(sql,Menu.class,pageInfo.getStartIndex(),pageInfo.getPageNum());
    }

    @Override
    public Integer count(Menu menu) {
        String sql = "SELECT count(1) FROM pn_menu p RIGHT JOIN pn_menu p2 ON p.mid=p2.parent_id WHERE p2.state = 1";
        return JDBCUtils.count(sql).intValue();
    }

    @Override
    public List<MenuType> findAllMenuType() {
        String sql = "SELECT mid,menu_name FROM pn_menu WHERE parent_id = 0 and state = 1";
        return JDBCUtils.find(sql,MenuType.class);
    }

    @Override
    public Boolean updateMenu(Menu menu) {
        String sql = "UPDATE pn_menu SET menu_icon=?,menu_name=?,menu_url=?,parent_id=?,state=? WHERE mid=?";
        return JDBCUtils.update(sql,menu.getMenu_icon(),menu.getMenu_name(),menu.getMenu_url(),menu.getParent_id(),menu.getState(),menu.getMid())>0;

    }

    @Override
    public Boolean addMenu(Menu menu) {
        String sql = "INSERT INTO pn_menu VALUES (null,?,?,?,?,?)";
        return JDBCUtils.update(sql,menu.getMenu_icon(),menu.getMenu_name(),menu.getMenu_url(),menu.getParent_id(),menu.getState())>0;
    }

    @Override
    public Boolean deleteMenu(Integer id) {
        String sql = "UPDATE pn_menu SET state=0 WHERE mid=?";
        return JDBCUtils.update(sql,id)>0;
    }

    public static void main(String[] args) {
        List<MenuInfo> menuInfos = new MenuInfoDaoImpl().showUserMenuByRoleMinId(1);
        System.out.println(menuInfos);
    }

}


